package cn.iune.web.erp.controllers;

import java.util.List;
import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.justep.baas.Utils;

import cn.iune.config.SecurityService;
import cn.iune.erp.baseinfo.IFileFactoryService;
import cn.iune.erp.baseinfo.ISettleCategoryService;
import cn.iune.erp.baseinfo.SettleCategory;
import cn.iune.erp.setting.IParamSettingService;



@Controller
@RequestMapping(value="/baseinfo")
public class BaseinfoController {
	
	private final Logger log = LogManager.getLogger(this);
	
	@Autowired
	private IFileFactoryService fileFactoryService;
	
	@Autowired
	private IParamSettingService paramSettingService;
	

	
	@RequestMapping(value="/fileFactory/list",method=RequestMethod.GET)
	public ModelAndView fileFactoryList(){
		ModelAndView mav = new ModelAndView("/appcenter/baseinfo/fileFactory/list");
		return mav;
	}

	@RequestMapping(value="/fileFactory/single",method=RequestMethod.GET)
	public ModelAndView fileFactorySingle(){
		ModelAndView mav = new ModelAndView("/appcenter/baseinfo/fileFactory/single");
		return mav;
	}

	//////////////////////////对应前台BizData的接口////////////////////////////////
	//增
	@RequestMapping(value={"/fileFactory/list/create","/fileFactory/single/create"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject fileFactoryCreate(@RequestBody JSONObject data) {
		System.out.println("controller.data-->>"+data.toJSONString());
		JSONObject params = JSONObject.parseObject(data.toJSONString());
		//增加登录信息
		//缺省值
		JSONArray defaultValues = params.getJSONArray("defaultValues");
		//循环缺省值（多行）
		for(int i=0;i<defaultValues.size();i++) {
			JSONObject rowValue = defaultValues.getJSONObject(i);
			//循环列
			//循环缺省值（单行）
			for(String col: rowValue.keySet()) {
				System.out.println("controller.defaultValues-->>"+col+":"+rowValue.get(col));
				if(":getCurrentDate()".equals(rowValue.get(col))) {
					rowValue.put(col, Utils.getCurrentDate());
				}
				if(":getCurrentPsmFID()".equals(rowValue.get(col))) {
					rowValue.put(col, SecurityService.getCurrentPsm().getsFID());
				}
				if(":getCurrentPsmFName()".equals(rowValue.get(col))) {
					rowValue.put(col, SecurityService.getCurrentPsm().getsFName());
				}
				if(":getCurrentDateTime()".equals(rowValue.get(col))) {
					rowValue.put(col, Utils.getCurrentDateTime());
				}
				if(":getCurrentPersonCode()".equals(rowValue.get(col))) {
					rowValue.put(col, SecurityService.getCurrentPerson().getString("cUsercode"));
				}
				if(":getCurrentPersonName()".equals(rowValue.get(col))) {
					rowValue.put(col, SecurityService.getCurrentPerson().getString("cUsername"));
				}
				if(":getCurrentPersonID()".equals(rowValue.get(col))) {
					rowValue.put(col, SecurityService.getCurrentPerson().getString("userId"));
				}
				if(":getCurrentOgnFID()".equals(rowValue.get(col))) {
					rowValue.put(col, SecurityService.getCurrentOgn().getsFID());
				}
				if(":getCurrentOgnLongName()".equals(rowValue.get(col))) {
					rowValue.put(col, SecurityService.getCurrentOgn().getsLongName());
				}
				System.out.println("controller.defaultValues-->>"+col+":"+rowValue.get(col));
			}
			
		}

		JSONObject json = fileFactoryService.newData(params);
		return json;
	}

	//删
	@RequestMapping(value= {"/fileFactory/list/delete","/fileFactory/single/delete"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject fileFactoryDelete(@RequestBody JSONObject data) {
		return fileFactoryService.deleteData(data);
	}

	//改
	@RequestMapping(value= {"/fileFactory/list/save","/fileFactory/single/save"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject fileFactorySave(@RequestBody JSONObject object) {
		return fileFactoryService.saveData(object);
	}

	//查
	@RequestMapping(value= {"/fileFactory/list/query","/fileFactory/single/query"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject fileFactoryQuery(@RequestBody JSONObject data) {
		log.info("data-->>"+data.toJSONString());
		data = SecurityService.addDataAccessPerm(data);
		return fileFactoryService.queryData(data);
	}
	
	//验证数据合法性
	@RequestMapping(value= {"/fileFactory/list/valid","/fileFactory/single/valid"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject fileFactoryValid(@RequestBody JSONObject data) {
		return fileFactoryService.validData(data);
	}
	//////////////////////////对应前台BizData的接口////////////////////////////////

	@RequestMapping(value="/fileFactory/selectSingle",method=RequestMethod.GET)
	public ModelAndView fileFactorySelectSingle(){
	
		ModelAndView mav = new ModelAndView("/appcenter/baseinfo/fileFactory/selectSingle");
		return mav;
	}
	
	/*
	@RequestMapping(value="/fileFactory/lists",method=RequestMethod.POST)
	@ResponseBody
	public List<FileFactory> fileFactoryLists() {
		return fileFactoryService.getAll();
	}
	*/
	
	
	@Autowired
	private ISettleCategoryService settleCategoryService;
	
	@RequestMapping(value="/settleCategory/list",method=RequestMethod.GET)
	public ModelAndView settleCategoryList(){
		ModelAndView mav = new ModelAndView("/appcenter/baseinfo/settleCategory/list");
		return mav;
	}

	@RequestMapping(value="/settleCategory/single",method=RequestMethod.GET)
	public ModelAndView settleCategorySingle(){
	
		ModelAndView mav = new ModelAndView("/appcenter/baseinfo/settleCategory/single");
		return mav;
	}
	
	@RequestMapping(value={"/settleCategory/list/create"},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject settleCategoryCreate(@RequestBody JSONObject data, HttpServletRequest request) {
		data = SecurityService.translateFun(data);
		JSONObject json = settleCategoryService.newData(data);
		return json;
	}

	@RequestMapping(value="/settleCategory/list/query",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject settleCategoryQuery(@RequestBody JSONObject data) {
//		data = SecurityService.addDataAccessPerm(data);
		return settleCategoryService.queryData(data);
	}

	@RequestMapping(value="/settleCategory/list/save",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject settleCategorySave(@RequestBody JSONObject object) {
		return settleCategoryService.saveData(object);
	}

	@RequestMapping(value="/settleCategory/list/delete",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject settleCategoryDelete(@RequestBody JSONObject data) {
		return settleCategoryService.deleteData(data);
	}

	@RequestMapping(value="/settleCategory/queryList",method=RequestMethod.GET)
	@ResponseBody
	public JSONObject settleCategoryQueryList()  {
//		String enterpriseNo = SecurityService.getCurrentOgn().getsFID();
		JSONObject data = new JSONObject();
		/*
		JSONObject columns = data.getJSONObject("columns"); // 列定义
		String orderBy = data.getString("orderBy"); // 排序
		Integer limit = data.getInteger("limit"); // 分页查询的行数
		Integer offset = data.getInteger("offset"); // 分页查询的行偏移
		String search = data.getString("search"); // 检索关键字
		String filter = data.getString("filter"); // 过滤条件
		String id = data.getString("id");
		*/
		
		JSONObject columns = new JSONObject();
		columns = Utils.entityToDefColJSON(SettleCategory.class);
		data.put("columns", columns);
		data.put("limit", -1);
		data.put("offset", 0);
//		String filter = "SettleInfoOrg.enterpriseNo='"+enterpriseNo+"'";
//		data.put("filter", filter);
		data.put("orderBy", "SettleInfoOrg.sortNo ASC");

		return settleCategoryService.queryListJDBC(data);
	}
    
	@RequestMapping(value="/settleCategory/getList",method=RequestMethod.GET)
	@ResponseBody
	public JSONObject getSettleInfoOrgList()  {
		String enterpriseNo = SecurityService.getCurrentOgn().getsFID();
		List<SettleCategory> list = settleCategoryService.getList(enterpriseNo);
		JSONArray rows = new JSONArray();
		for(SettleCategory obj: list) {
			JSONObject row = new JSONObject();
			row.put("fID", obj.getfID());
			row.put("settleCode", obj.getSettleCode());
			row.put("settleName", obj.getSettleName());
			rows.add(row);
		}

		JSONObject json = new JSONObject();
		json.put("code", 0);
		json.put("msg", "success");
		json.put("data", rows);
		
		return json;
	}
  
	
	//设置共用参数--系统参数设置
	@RequestMapping(value="/paramSetting/common",method=RequestMethod.GET)
	public ModelAndView entryCGRK(){
		ModelAndView mav = new ModelAndView("/appcenter/baseinfo/paramSetting/common");
		return mav;
	}
	
	@RequestMapping(value={
			"/paramSetting/common/create",
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject paramSettingCreate(@RequestBody JSONObject data) {
		data = SecurityService.translateFun(data);
		JSONObject json = paramSettingService.newData(data);
		return json;
	}
	
	@RequestMapping(value={
			"/paramSetting/common/query"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject paramSettingQuery(@RequestBody JSONObject data) {
		data = SecurityService.addDataAccessPerm(data);
		return paramSettingService.queryData(data);
	}

	@RequestMapping(value={
			"/paramSetting/common/save"
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject paramSettingSave(@RequestBody JSONObject object) {
		return paramSettingService.saveData(object);
	}
	
	@RequestMapping(value= {
			"/paramSetting/common/delete",
			},method=RequestMethod.POST)
	@ResponseBody
	public JSONObject paramSettingDelete(@RequestBody JSONObject data) {
		return paramSettingService.deleteData(data);
	}


	
	
}
